package com.meixiang.adapter.service;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.meixiang.R;
import com.meixiang.entity.CircleImage;
import com.meixiang.recyclerview.RecyclerAdapter;
import com.meixiang.util.DensityUtil;
import com.meixiang.view.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * author:xiejingwen
 */
public class CircleImageRecyclerAdapter extends RecyclerAdapter<CircleImageRecyclerAdapter.ViewHolder> {
    public List<CircleImage> data=new ArrayList<>();
    private Context mContext;
    private Activity activity;
    public CircleImageRecyclerAdapter(Activity activity){
       this.activity=activity;
    }
    public void addData(List<CircleImage> data){
        this.data.addAll(data);
        notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext=parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_reseevation, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if (!data.get(position).isSelect()){
            holder.mIvSelect.setVisibility(View.GONE);
            holder.mCivHead.setBorderColor(ContextCompat.getColor(mContext,R.color.gray_d));
            holder.mCivHead.setBorderWidth(1);
        }else{
            holder.mIvSelect.setVisibility(View.VISIBLE);
            holder.mCivHead.setBorderColor(ContextCompat.getColor(mContext,R.color.green_tab));
            holder.mCivHead.setBorderWidth(DensityUtil.dip2px(mContext,1));
        }
        Glide.with(activity).load(data.get(position).getPhotoImage())
                .asBitmap()
                .placeholder(R.drawable.image_default_gray_bg)
                .error(R.drawable.image_default_gray_bg)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        holder.mCivHead.setImageBitmap(resource);
                    }
                });

    }


    @Override
    public int getItemCount() {
        return data.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.civ_head)
        CircleImageView mCivHead;
        @Bind(R.id.iv_select)
        ImageView mIvSelect;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
